// pages/teacherKaoqinInfor/teacherKaoqinInfor.js
var api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date1:'',
    date2:'',
    rangeKey1:'campus_name',
    rangeKey2:'name',
    value1:'校区',
    value2:'班级',
    kaoqinList:[],
    hidden: true,
  },
  cancel: function () {
    this.setData({
      hidden: true
    });
  },
  confirm(){
    this.setData({
      hidden: true
    });
  },

  //获取校区、项目、班级
  getSchool() {
    api.fetchApi('teacher/getbasic', { token: wx.getStorageSync('token') }).then(res => {
      var campusArr = res.campus;
      var classArr = res.class;
      var projectArr = res.project;
      //console.log(campusArr, classArr, projectArr)
      this.setData({
        campusArr,
        classArr,
        projectArr
      })
    })
  },

//选择学校
selectSchool(e){
  var campusArr = this.data.campusArr;
  var rangeKey1 = this.data.rangeKey1;
  var nowArr = campusArr[e.detail]
  console.log(nowArr)
  this.setData({
    value1: nowArr[rangeKey1],
    campus: nowArr.id
  })

this.setData({
  kaoqinList: []
})
  this.getKaoqinInfor(1)
},

//选择班级
  selectClass(e){
    var classArr = this.data.classArr;
    var rangeKey2 = this.data.rangeKey2;
    var nowArr = classArr[e.detail]
    console.log(nowArr)
    this.setData({
      value2: nowArr[rangeKey2],
      placement_id: nowArr.id
    })
    this.setData({
      kaoqinList: []
    })
    this.getKaoqinInfor(1)
    
  },

  //获取考勤信息
getKaoqinInfor(page){
  var params={
    token: wx.getStorageSync('token'),
    campus: this.data.campus,
    placement_id: this.data.placement_id,
    time: this.data.date1 + '~' + this.data.date2,
    page
  }
  api.fetchApi('teacher/attendance', params).then(res=>{
    var kaoqinList = this.data.kaoqinList,
        kaoqinList1 = res.data,
        conut1=res.count;
    console.log(kaoqinList1)
    if (kaoqinList1.length == 0 && page > 1) {
      //api.showToast('到底啦!', 'http://qnwmfx.laigl.com/iconimg/warn.png', 1500)
      this.setData({
        isMore: true
      })
      return
    }
    kaoqinList = [...kaoqinList, ...kaoqinList1]
    this.setData({
      kaoqinList,
      page,
      conut1
    })
  })
},

//查看详情
  details(e){
   
    console.log(e)
    let id = e.currentTarget.dataset.id;
    api.fetchApi('Teacher/att_info', { id, token: wx.getStorageSync('token')}).then(res=>{
      console.log(res)
      var studentArr=res;
      this.setData({
        studentArr
      })

      api.fetchApi('Teacher/att_num', { id, token: wx.getStorageSync('token') }).then(res => {
        let { leave_num, really_num, should_num, truancy_num, untreated_num } = res;
        this.setData({
          leave_num,
          really_num,
          should_num,
          truancy_num,
          untreated_num,
          hidden: false
        })
      })
      
    })

  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var date1 = new Date().dateFormat('Y-m-d');
    var date2 = (new Date(new Date().valueOf() + 86400000)).dateFormat('Y-m-d')
    console.log(date1, date2)
    this.setData({
      date1,
      date2
    })
    
    this.getSchool()
    this.getKaoqinInfor(1);

   
  },
  //时间始   子组件传过来的事件
  dateChange1(e){
    
    this.setData({
      date1:e.detail
    })
    this.setData({
      kaoqinList: []
    })
    this.getKaoqinInfor(1)
  },

//时间止
  dateChange2(e) {
 
    this.setData({
      date2: e.detail
    })
    this.setData({
      kaoqinList: []
    })

    this.getKaoqinInfor(1)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page = this.data.page || 1;
    page++;
   
    this.getKaoqinInfor(page)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})